package com.ruoyi.client.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.employer.domain.CargoGoods;
import com.ruoyi.employer.domain.CargoSourceDetails;
import com.ruoyi.employer.mapper.CargoGoodsMapper;
import com.ruoyi.employer.mapper.CargoSourceDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.client.mapper.CargoUserOrdersMapper;
import com.ruoyi.client.domain.CargoUserOrders;
import com.ruoyi.client.service.ICargoUserOrdersService;

/**
 * 货主订单Service业务层处理
 * 
 * @author adf
 * @date 2024-08-17
 */
@Service
public class CargoUserOrdersServiceImpl implements ICargoUserOrdersService 
{
    @Autowired
    private CargoUserOrdersMapper cargoUserOrdersMapper;
    @Autowired
    private CargoSourceDetailsMapper cargoSourceDetailsMapper;
    @Autowired
    private CargoGoodsMapper cargoGoodsMapper;

    /**
     * 查询货主订单
     * 
     * @param id 货主订单主键
     * @return 货主订单
     */
    @Override
    public CargoUserOrders selectCargoUserOrdersById(Long id)
    {
        CargoUserOrders cargoUserOrders = cargoUserOrdersMapper.selectCargoUserOrdersById(id);
        //根据货源id查询货源详情
        Long cargoSourceDetailsId = cargoUserOrders.getCargoSourceDetails();
        CargoSourceDetails cargoSourceDetails = cargoSourceDetailsMapper.selectCargoSourceDetailsById(cargoSourceDetailsId);
        cargoUserOrders.setCargoSource(cargoSourceDetails);

        //根据货货物id查询货物详情
        Long goodsId = cargoUserOrders.getCargoGoods().getId();
        CargoGoods cargoGoods = cargoGoodsMapper.selectCargoGoodsById(goodsId);
        cargoUserOrders.setCargoGoods(cargoGoods);

        return cargoUserOrders;

    }

    /**
     * 查询货主订单列表
     * 
     * @param cargoUserOrders 货主订单
     * @return 货主订单
     */
    @Override
    public List<CargoUserOrders> selectCargoUserOrdersList(CargoUserOrders cargoUserOrders)
    {
        return cargoUserOrdersMapper.selectCargoUserOrdersList(cargoUserOrders);
    }

    /**
     * 新增货主订单
     * 
     * @param cargoUserOrders 货主订单
     * @return 结果
     */
    @Override
    public int insertCargoUserOrders(CargoUserOrders cargoUserOrders)
    {
        //根据日期生成运单号
        String dateTimeNow = DateUtils.dateTimeNow();

        CargoUserOrders newCargoUserOrders = new CargoUserOrders();
        newCargoUserOrders.setNumber(dateTimeNow); // 生成运单号
        newCargoUserOrders.setOrderTime(LocalDateTime.now()); // 生成当前日期时间：订单生成时间
        newCargoUserOrders.setPrice(cargoUserOrders.getPrice()); // 运费
        newCargoUserOrders.setLoadingTime(cargoUserOrders.getLoadingTime()); // 装车时间
        newCargoUserOrders.setUnloadTime(cargoUserOrders.getUnloadTime()); // 卸车时间
        newCargoUserOrders.setPayMethod(cargoUserOrders.getPayMethod()); // 支付方式
        newCargoUserOrders.setCollectionAccountId(cargoUserOrders.getCollectionAccountId()); // 收款账户
        newCargoUserOrders.setCargoSourceDetails(cargoUserOrders.getCargoSourceDetails()); // 货物来源
        newCargoUserOrders.setCarownerCardetials(cargoUserOrders.getCarownerCardetials()); // 车辆详情
        newCargoUserOrders.setStatus(1L); // 订单状态：1-待付款
        int i = cargoUserOrdersMapper.insertCargoUserOrders(newCargoUserOrders);
        // 判断
        if (i > 0) {
            // 修改货源状态为已下单
            CargoSourceDetails cargoSourceDetails = new CargoSourceDetails();
            cargoSourceDetails.setId(cargoUserOrders.getCargoSourceDetails());
            cargoSourceDetails.setStatus("1");
            int num = cargoSourceDetailsMapper.updateCargoSourceDetails(cargoSourceDetails);
            if (num > 0) {
                System.out.println("货源状态修改成功");
            } else {
                System.out.println("货源状态修改失败");
            }
        }
        return i;
    }

    /**
     * 修改货主订单
     * 
     * @param cargoUserOrders 货主订单
     * @return 结果
     */
    @Override
    public int updateCargoUserOrders(CargoUserOrders cargoUserOrders)
    {
        return cargoUserOrdersMapper.updateCargoUserOrders(cargoUserOrders);
    }

    /**
     * 批量删除货主订单
     * 
     * @param ids 需要删除的货主订单主键
     * @return 结果
     */
    @Override
    public int deleteCargoUserOrdersByIds(Long[] ids)
    {
        return cargoUserOrdersMapper.deleteCargoUserOrdersByIds(ids);
    }

    /**
     * 删除货主订单信息
     * 
     * @param id 货主订单主键
     * @return 结果
     */
    @Override
    public int deleteCargoUserOrdersById(Long id)
    {
        return cargoUserOrdersMapper.deleteCargoUserOrdersById(id);
    }
}
